home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
1833
/
1833.xpi
/
chrome
/
yoono.jar
/
content
/
yoono
/
servicesweb.js
< prev
next >
Wrap
Text File
|
2009-12-16
|
12KB
|
300 lines
function ynConnectException(msg){
this.message = msg;
this.toString = function (){return this.message};
}
function ynResponseException(msg){
this.message = msg;
this.toString = function (){return this.message};
}
var ynWebServices = {
_serverUrl : '',
_sendRequest : function (script, libelle){
// results is an array with request and xml result if successfull
var results = null;
YOONO_LOG.debug('Sending ' + libelle);
try{
results = YOONO_CMPT.sendRequest(this._serverUrl, 'POST', 'sync', script, null);
} catch (e) {
throw new ynConnectException(e.toString());
}
if('' == results[1]) {
throw new ynResponseException(result[0].status);
}
return results[1];
},
verifyLogin : function(login, passwd){
var script = <server-script version="1.0">
<context>
<user-id> {login + ':' + passwd} </user-id>
<locale>{YOONO_UTILS.getLocale()}</locale>
<version>{YOONO_UTILS.getExtensionVersion()}</version>
<client>xpi</client>
</context>
<connect/>
</server-script>;
var rep = this._sendRequest(script,"verifyLogin");
// connection OK
if (rep['display-message'].@code == 'MSG_WELCOME') {
YNPREFBRANCH.setCharPref('userid', login + ':' + passwd);
YOONO_MEMO.fixLayout(window);
return true;
}else{
// login password invalid
return false;
}
},
getNewUserId : function(){
var script = <server-script version="1.0">
<connect />
<context>
<locale>{YOONO_UTILS.getLocale()}</locale>
<version>{YOONO_UTILS.getExtensionVersion()}</version>
<client>xpi</client>
</context>
</server-script>;
var rep = this._sendRequest(script,"getNewUserId");
if(rep['display-message'].@code == 'MSG_WELCOME_NEW_USER') {
var ac = YNPREFBRANCH.getIntPref('anonymous.creations');
YNPREFBRANCH.setIntPref('anonymous.creations', ac + 1);
YNPREFBRANCH.setIntPref('anonymous.creation.date', Math.round(new Date().getTime()/1000)) ;
return rep.context['user-id'].toString();
} else {
// login password invalid
return false;
}
},
checkLoginDispo : function(pseudo){
var script= <server-script version="1.0"><get-user-data user-id={pseudo}/></server-script> ;
var rep = this._sendRequest(script,"checkLoginDispo");
if (rep['display-error'].@code == 'ERROR_INVALID_USER_ID')
return true;
else
return false;
},
// @param nsIFile file
sendAvatar : function (userid, file) {
// from http://xulfr.org/wiki/ApplisWeb/Request
var boundary="111222111";
const STRINGIS="@mozilla.org/io/string-input-stream;1";
const nsIStringInputStream=Components.interfaces.nsIStringInputStream;
var fin=Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
var buf=Components.classes["@mozilla.org/network/buffered-input-stream;1"]
.createInstance(Components.interfaces.nsIBufferedInputStream);
fin.init(file,0x01,0444,0);
buf.init(fin,4096);
var hsis=Components.classes[STRINGIS].createInstance(nsIStringInputStream);
var sheader=new String();
sheader+="\r\n";
sheader+="--"+boundary+"\r\nContent-disposition: form-data;name=\"user-id\"\r\n\r\n";
sheader+=userid;
sheader+="\r\n"+"--"+boundary+"\r\n";
sheader+="Content-disposition: form-data;name=\"image\";filename=\""+file.leafName+"\"\r\n";
sheader+="Content-Type: application/octet-stream\r\n";
sheader+="Content-Length: "+file.fileSize+"\r\n\r\n";
hsis.setData(sheader,sheader.length);
var endsis=Components.classes[STRINGIS].createInstance(nsIStringInputStream);
var bs=new String("\r\n--"+boundary+"--\r\n");
endsis.setData(bs,bs.length);
var mis=Components.classes["@mozilla.org/io/multiplex-input-stream;1"]
.createInstance(Components.interfaces.nsIMultiplexInputStream);
mis.appendStream(hsis);
mis.appendStream(buf);
mis.appendStream(endsis);
YOONO_LOG.debug("\n==========================-->sendAvatar");
var xmlr = new XMLHttpRequest();
xmlr.open("POST",YNPREFBRANCH.getCharPref('serverurl') + "avatar",false);
xmlr.setRequestHeader("Content-Length",(mis.available()-2));
xmlr.setRequestHeader("Content-Type","multipart/form-data; boundary="+boundary);
xmlr.send(mis);
var imgValid;
YOONO_LOG.debug("\n"+xmlr.responseText);
var xml = new XML(xmlr.responseText.replace(/<\?xml.*?\?>\n?/g, ""));
if( xmlr.responseText.indexOf("INVALID_AVATAR") != -1 ) {
return false;
} else {
return xml['import-user-data']['user-data'].avatar.toString();
}
},
// that's called only for anonymous to registered account transition.
changeLoginPassword: function (userid, login, password){
var script = <server-script version="1.0"/>;
script.appendChild( <context> <user-id> {userid} </user-id> </context>);;
script.appendChild( <change-login-password login={login} password={password}/>);
var rep = this._sendRequest(script, "changeLoginPassword");
if (rep['display-message'].@code == 'MSG_LOGIN_PASSWORD_CHANGED') {
var uc = YNPREFBRANCH.getIntPref('user.creations');
YNPREFBRANCH.setIntPref('user.creations', uc + 1);
YNPREFBRANCH.setIntPref('user.creation.date', Math.round(new Date().getTime()/1000)) ;
return true;
} else {
return false;
}
},
// Get user info, synchronously, for login/synch wizard
getUserData: function (userdata) {
var script = <server-script version="1.0"/> ;
script.appendChild( <context> <user-id>{userdata.userid}</user-id> </context>);
script.appendChild( <get-user-data/>);
var rep = this._sendRequest(script, "getUserData");
if (rep['display-error'].@code == 'ERROR_INVALID_USER_ID') {
return false;
}
var data = rep['import-user-data']['user-data'];
userdata.pseudo = userdata.login;
userdata.name = data['full-name'].toString();
userdata.location = data['location'].toString();
userdata.born = data['born'].toString();
userdata.website = data['website'].toString();
// renseigner par defaut le champ avec l'URL de "Mon espace perso"
if (!userdata.website) {
userdata.website = 'http://www.yoono.com/favorites.jsp?user-id=' + userdata.login;
}
// Avatar in xml can be empty for old user who has an avatar !
userdata.avatar = data['avatar'].toString();
userdata.description = data['message'].toString();
userdata.passions = data['interests'].toString();
userdata.email = data['email'].toString();
if (data['email'].@perm.toString().toLowerCase() == "owner") {
userdata.privateemail = true;
}else{
userdata.privateemail = false;
}
userdata.skype = data['skype-id'].toString();
userdata.msn = data['msn-id'].toString();
userdata.icq = data['icq-id'].toString();
userdata.aim = data['aim-id'].toString();
return true;
},
saveUserEmailAvatar : function(userData){
var script = <server-script version="1.0">
<context client-area="/"><user-id>{userData.userid}</user-id></context>
<save-user-data> <user-data>
<email perm="OWNER">{userData.email}</email>
<avatar>{userData.avatar}</avatar>
</user-data></save-user-data></server-script>;
var rep = this._sendRequest(script, "saveUserEmailAvatar");
if (rep['display-message'].@code == 'MSG_USER_DATA_SAVED') {
return true;
}else{
return false;
}
},
saveUserData : function(infos){
var userdata = <user-data>
{(infos.name ? <full-name>{infos.name}</full-name> : '')}
{(infos.born ? <born>{infos.born}</born> : '')}
{(infos.location ? <location>{infos.location}</location> : '')}
{(infos.website ? <website>{infos.website}</website> : '')}
{(infos.description ? <message>{infos.description}</message> : '')}
{(infos.passions ? <interests>{infos.passions}</interests> : '')}
{(infos.email ? <email perm={infos.privateemail?"owner":""}>{infos.email}</email> : '')}
{(infos.skype ? <skype-id>{infos.skype}</skype-id> : '')}
{(infos.msn ? <msn-id>{infos.msn}</msn-id> : '')}
{(infos.icq ? <icq-id>{infos.icq}</icq-id> : '')}
{(infos.aim ? <aim-id>{infos.aim}</aim-id> : '')}
{(infos.avatar ? <avatar>{infos.avatar}</avatar> : '')}
</user-data>;
var script = <server-script version="1.0">
<context client-area="/"><user-id>{infos.userid}</user-id></context>
<save-user-data>{userdata}</save-user-data></server-script>;
var rep = this._sendRequest(script, "saveUserData");
if (rep['display-message'].@code == 'MSG_USER_DATA_SAVED') {
return true;
}else{
return false;
}
},
getSharedLink : function ( userid ) {
var script = <server-script version="1.0">
<context><user-id>{userid}</user-id></context>
<load-all-links published="true"/>
</server-script>;
var rep = this._sendRequest(script, "getUserPublishedInfos");
return rep['import-links'].elements('link');
},
updateSharedLink : function (userid, newLinksToShare, linksToRemove){
var nbr = this.getPublishedCurrentMaxNumber(userid);
var script = <server-script version="1.0">
<context client-area="/"><user-id>{userid}</user-id>
</context></server-script>;
for (var i in newLinksToShare) {
nbr++;
var item = newLinksToShare[i].node.getPath().toServer;
script.appendChild(<add-xlink ref={":" + nbr} path={"/$pub/" +nbr}/>)
var tmp = <update-link/>;
tmp.appendChild(<old-link path={item}/>);
tmp.appendChild(<new-link path={item} id={nbr}/>);
script.appendChild(tmp);
}
for (var i in linksToRemove) {
var item = linksToRemove[i];
script.appendChild(<remove-xlink ref={":" + item.publishedNbr} path={"/$pub/" +item.publishedNbr}/>)
var tmp = <update-link/>;
tmp.appendChild(<old-link path={item.node.getPath().toServer}/>);
tmp.appendChild(<new-link path={item.node.getPath().toServer} id={item.publishedNbr}/>);
script.appendChild(tmp);
}
var rep = this._sendRequest(script, "updateSharedLink");
return true;
},
getPublishedCurrentMaxNumber : function(userid) {
var script = <server-script version="1.0">
<context> <user-id> {userid} </user-id> </context>
<load-all-xlinks/>
</server-script>;
var rep = this._sendRequest(script, "getPublishedCurrentMaxNumber");
var links = rep['import-links'].elements('link');
var nbr = 0;
for (var i = links.length(); i-- > 0;) {
if (links[i].@path.toString().beginsWith('/$net'))
continue;
var ref = links[i].@ref.toString();
var id = parseInt(ref.slice(ref.indexOf(':') + 1));
if (id > nbr) nbr = id;
}
return nbr;
}
}
ynWebServices._serverUrl = YNPREFBRANCH.getCharPref('serverurl')+ 'linkserver';